package com.ruoyi.patrol.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.PatrolConstans;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtLineGuard;
import com.ruoyi.patrol.domain.PtLineSite;
import com.ruoyi.patrol.domain.PtSite;
import com.ruoyi.patrol.mapper.PtLineSiteMapper;
import com.ruoyi.patrol.service.IPtLineSiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtLineMapper;
import com.ruoyi.patrol.domain.PtLine;
import com.ruoyi.patrol.service.IPtLineService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 巡查线路Service业务层处理
 * 
 * @author lkm
 * @date 2022-11-07
 */
@Service
public class PtLineServiceImpl implements IPtLineService 
{
    @Autowired
    private PtLineMapper ptLineMapper;
    @Autowired
    private IPtLineSiteService ptLineSiteService;
    @Autowired
    private PtLineSiteMapper ptLineSiteMapper;

    /**
     * 查询巡查线路
     * 
     * @param id 巡查线路ID
     * @return 巡查线路
     */
    @Override

    public PtLine selectPtLineById(Long id)
    {
        PtLine line = ptLineMapper.selectPtLineById(id);
        List<PtLineSite> lineSites = ptLineSiteMapper.selectPtLineSiteByLineId(line.getId());
        line.setLineSites(lineSites);

        return line;
    }

    @Override
    @DataScope(deptAlias = "l")
    public List<PtLine> selectAll(PtLine ptLine) {
        return ptLineMapper.selectAll(ptLine);
    }

    @Override
    public boolean isLineInUse(Long lineId) {
        PtLine line = ptLineMapper.selectPtLineById(lineId);
        if (line.getLineType() == PatrolConstans.LINE_TYPE_GUARD) {
            return ptLineMapper.isLineInUseForGuard(lineId);
        } else if (line.getLineType() == PatrolConstans.LINE_TYPE_STAFF) {
            return ptLineMapper.isLineInUseForStaff(lineId);
        }
        return false;
    }

    /**
     * 查询巡查线路列表
     * 
     * @param ptLine 巡查线路
     * @return 巡查线路
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @DataScope(deptAlias = "l")
    public List<PtLine> selectPtLineList(PtLine ptLine)
    {
        List<PtLine> lines = ptLineMapper.selectPtLineList(ptLine);

        for (PtLine line : lines) {
            List<PtLineSite> lineSites = ptLineSiteMapper.selectPtLineSiteByLineId(line.getId());
            line.setLineSites(lineSites);
        }

        return lines;
    }

    @Override
    public boolean siteNameExist(PtLine ptLine) {
        return ptLineMapper.selectFirstPtLineByNameAndSchoolId(ptLine) == null ? false : true;
    }
    /**
     * 新增巡查线路
     * 
     * @param ptLine 巡查线路
     * @return 结果
     */
    @Override
    public int insertPtLine(PtLine ptLine)
    {
        ptLine.setCreateTime(DateUtils.getNowDate());
        if (CollectionUtils.isEmpty(ptLine.getLineSites())) {
            return 0;
        } else {
            ptLine.setSiteCount(ptLine.getLineSites().size());
        }
        ptLineMapper.insertPtLine(ptLine);

        //设置线路ID
        ptLine.getLineSites().stream().forEach(e -> e.setLineId(ptLine.getId()));
        return ptLineSiteService.batchInsertPtLineSite(ptLine.getLineSites());
    }

    /**
     * 修改巡查线路
     * 
     * @param ptLine 巡查线路
     * @return 结果
     */
    @Override
    public int updatePtLine(PtLine ptLine)
    {
        ptLine.setUpdateTime(DateUtils.getNowDate());
        ptLineMapper.updatePtLine(ptLine);

        ptLine.getLineSites().stream().forEach(e -> e.setLineId(ptLine.getId())); //设置线路ID
        ptLineSiteService.deletePtLineSiteByLineId(ptLine.getId());
        return ptLineSiteService.batchInsertPtLineSite(ptLine.getLineSites());
    }

    /**
     * 批量删除巡查线路
     * 
     * @param ids 需要删除的巡查线路ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deletePtLineByIds(Long[] ids)
    {
        ptLineSiteService.deletePtLineSiteByLineIds(ids);
        return ptLineMapper.deletePtLineByIds(ids);
    }

    /**
     * 删除巡查线路信息
     * 
     * @param id 巡查线路ID
     * @return 结果
     */
    @Override
    public int deletePtLineById(Long id)
    {
        return ptLineMapper.deletePtLineById(id);
    }
}
